package com.lancoo.znbkxx.teacher.ui.fragment

import android.content.DialogInterface
import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.base.BaseVMFragment
import com.lancoo.znbkxx.teacher.databinding.FragmentSkillCommentBinding
import com.lancoo.znbkxx.teacher.model.data.AnsweringSkill
import com.lancoo.znbkxx.teacher.model.data.HomeWorkAnalysis
import com.lancoo.znbkxx.teacher.utils.LancooUtils
import com.lancoo.znbkxx.teacher.viewmodel.HardSolutionViewModel

import kotlinx.android.synthetic.main.fragment_skill_comment.*
import kotlinx.android.synthetic.main.fragment_skill_comment.vp
import kotlinx.android.synthetic.main.item_tab_skill.view.*

/**
 * @author : Admin
 * @date   : 2020/10/28
 * @desc   :
 */
class SkillAnalysisFragment : BaseVMFragment<HardSolutionViewModel, FragmentSkillCommentBinding>(),
        View.OnClickListener {
    private val typeNo by lazy { requireArguments().getString("typeNo") }
    private val genreID by lazy { requireArguments().getString("genreID") }
    private val skillList = mutableListOf<AnsweringSkill>()
    private lateinit var selectAnswerSkillDialog: SelectAnswerSkillDialog
    override fun setLayoutID(): Int {
        return R.layout.fragment_skill_comment
    }

    override fun providerContext(): Any? {
        return activity
    }

    override fun providerVMClass(): Class<HardSolutionViewModel>? {
        return HardSolutionViewModel::class.java
    }

    override fun initView() {
        initViewPager(viewModel.skillList.value!!)
        tv_change.setOnClickListener(this)
    }

    private fun initViewPager(skillList: MutableList<AnsweringSkill>) {
        vp.adapter = object : FragmentStateAdapter(childFragmentManager, lifecycle) {
            override fun getItemCount(): Int {
                return skillList.size
            }

            override fun createFragment(position: Int): Fragment {
                return SkillDetailFragment().apply {
                    arguments = Bundle().apply {
                        putString("name",
                                skillList[position].KnowledgeName)
                        putParcelable("answerSkillList",
                                skillList[position])
                    }
                }
            }
        }
        TabLayoutMediator(tbl_skill, vp) { tab: TabLayout.Tab, i: Int ->
            tab.setCustomView(R.layout.item_tab_skill)
            tab.customView!!.tv.text = skillList[i].KnowledgeName
        }.attach()
    }

    private fun addSkill() {
        val list = arrayListOf<AnsweringSkill>()
        val codeList = arrayListOf<String>()
        viewModel.answerSkillList.value!!.forEach {
            if (it.AnswerSkillDataList[0].isSelect) {
                list.add(it)
                codeList.add(it.KnowledgeCode)
            }
        }
        viewModel.answerSkillComment.value!!.AnsweringSkills = codeList.joinToString("|")
        initViewPager(list)
    }

    override fun startObserve() {
        viewModel.answerSkillList.observe(this, Observer {
            selectAnswerSkillDialog = SelectAnswerSkillDialog(this)
            selectAnswerSkillDialog.arguments = bundleOf(Pair("answerSkill", it),
                    Pair("position", vp.currentItem))
            selectAnswerSkillDialog.show(childFragmentManager, "")
        })
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_change -> viewModel.getAnswerSkillList(typeNo!!, genreID!!)
            R.id.iv_close -> {
                if (selectAnswerSkillDialog.getTotalAddNum() < 1) {
                    MyToast.showToast("至少添加1个技巧讲解")
                    return
                }
                addSkill()
                selectAnswerSkillDialog.dismiss()
            }
        }
    }
}